/*
-------------------------------------------------------------------
BIE is Copyright 2001-2004 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
 */
/*
 * ServletTest.java
 *
 * Created on January 8, 2003, 4:09 PM
 */

package tests.jd.crypt;


import java.text.*;
import java.util.*;
import java.security.*;
import java.security.interfaces.*;
import java.security.spec.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.*;
import java.math.*;
import sun.misc.BASE64Encoder;
import java.sql.*;

/**
 *
 * @author  jdepons
 */
public class ServletTest {
    
    /** Creates a new instance of ServletTest */
    public ServletTest() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{
        //String numroutes = request.getParameter("numroutes");
        //String hostname = request.getParameter("hostname");
        // String versionnumber = request.getParameter("versionnumber");
        //String expday = request.getParameter("expday");
        //String expmonth =request.getParameter("expmonth");
        //String expyear = request.getParameter("expyear");
        
        String routeCount = "5";
        String hostname = "wd-013.wdi.com";
        String version = "4";
        String expday = "11";
        String expmonth ="11";
        String expyear = "2004";
        long created = new java.util.Date().getTime();
        
        String exp = expmonth + "/" + expday + "/" + expyear;
        
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
        java.util.Date d = sdf.parse(exp, new ParsePosition(0));
        long expirydate = d.getTime();
        
        String licKey = hostname + "|" + routeCount + "|" + version + "|" + expirydate + "|" + created;
/*
PublicExponent=10001
modulus=96142334799ba0417560828127f1fef7682059b107f828dfbc953cf5840f5d02433303f43f0743bb253a7ac5539ef9c1e9cc1038c3210315d331c8593d977ce70d9790ba98e565fe5158166dc1b36ae6587d8b8c4fded7687cf2e2983dcc98f81b6e79c11282fe65697b6f52844a9700331a151283681a726f85b8483f012e7e32040a4d0d7e6f6d21008afc5263ddd3ab638b7e6d74f68614752cb3a06f1d5dd416b7a46497e082a7da21e4b151edfbb69e1a9bfb7a77e2ffc048e5df76fb4d6855a2ca8a9865ecf8bcd47848fd104f67f91581151ef59b1f9483bdd2d2596484ba3c10ab679fe4c37b41d96a239ed4316079969beec783714e0aba8bdc96a5
PrivateExponent=874c924c7d91c2d1208ba83bde1239e53bd76664c625805bec7cb29f38b64d087c22a2def06f912d47abc800db9947dafcf6be6d1de5460dc7ff6a7aa19f4fb851b5a181c7d38fb6ce08d6f78e183d387af07bfd3c084f3460e8eb458af947b2edb0144dee6d7001672e23b74592a53d45d4aec8a8f5125ab695dc042de0d7d30257d2c048218daceba0b81c80be1532bcf4010013850ae23c59f53f2fd944cb5350ac5cc9e929928f649446a08221a77d6e7ca24ae5c05de0b2569877bf5e72cc8bdb075cb4baad9395f500db61b97c699fce815f391aca618cd1936449a5420675db672f7a144045fca014c2207fe549cd7e743bcd2076459e69f1cd50dca1
modulus=96142334799ba0417560828127f1fef7682059b107f828dfbc953cf5840f5d02433303f43f0743bb253a7ac5539ef9c1e9cc1038c3210315d331c8593d977ce70d9790ba98e565fe5158166dc1b36ae6587d8b8c4fded7687cf2e2983dcc98f81b6e79c11282fe65697b6f52844a9700331a151283681a726f85b8483f012e7e32040a4d0d7e6f6d21008afc5263ddd3ab638b7e6d74f68614752cb3a06f1d5dd416b7a46497e082a7da21e4b151edfbb69e1a9bfb7a77e2ffc048e5df76fb4d6855a2ca8a9865ecf8bcd47848fd104f67f91581151ef59b1f9483bdd2d2596484ba3c10ab679fe4c37b41d96a239ed4316079969beec783714e0aba8bdc96a5
 */
        
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
        new BigInteger("96142334799ba0417560828127f1fef7682059b107f828dfbc953cf5840f5d02433303f43f0743bb253a7ac5539ef9c1e9cc1038c3210315d331c8593d977ce70d9790ba98e565fe5158166dc1b36ae6587d8b8c4fded7687cf2e2983dcc98f81b6e79c11282fe65697b6f52844a9700331a151283681a726f85b8483f012e7e32040a4d0d7e6f6d21008afc5263ddd3ab638b7e6d74f68614752cb3a06f1d5dd416b7a46497e082a7da21e4b151edfbb69e1a9bfb7a77e2ffc048e5df76fb4d6855a2ca8a9865ecf8bcd47848fd104f67f91581151ef59b1f9483bdd2d2596484ba3c10ab679fe4c37b41d96a239ed4316079969beec783714e0aba8bdc96a5", 16),
        new BigInteger("10001", 16));
        
        java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
        PublicKey pubKey = fact.generatePublic(pubKeySpec);
        
        Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
        c.init(Cipher.ENCRYPT_MODE, pubKey, new SecureRandom());
        byte[] out = c.doFinal(licKey.getBytes());
        
        BASE64Encoder encoder = new BASE64Encoder();
        String licenseText = encoder.encode(out);
        
        System.out.println("License = " + licenseText);
        System.out.println("len = " + licenseText.length());
        
        
        String sql = "insert into BIELicense";
        sql += " (hostName, routeCount, expiration, created, version, LicenseKey)";
        sql += " values ('" + hostname + "'," + routeCount + "," +expirydate + "," + created + "," + version + ",'" + licenseText + "')";
                
        System.out.println("sql = " + sql);
        
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager.getConnection("jdbc:mysql://scorpion.wdi.com/jdepons/?user=jdepons&password=green19");
        Statement stmt = conn.createStatement();
        stmt.execute(sql);
        conn.close();
     
        
        
    }    
}
